package ext.acae2.util;

import java.util.HashMap;
import java.util.Map;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import wt.log4j.LogR;

public class ParseDepartmentConfig {
	
	private static final Logger LOGGER = LogR.getLogger(ParseDepartmentConfig.class
			.getName());

	public static Map<String, String> execute(String path) throws Exception {
		
		Map<String, String> map = new HashMap<String, String>();

		XPathFactory pathFactory = XPathFactory.newInstance();
		XPath xpath = pathFactory.newXPath();
		XPathExpression pathExpression = xpath
				.compile("//DepartmentConfig/Department");

		Document doc = LoadingXmlUtil.execute(path);
		Object result = pathExpression.evaluate(doc, XPathConstants.NODESET);

		NodeList nodes = (NodeList) result;
		for (int i = 0; i < nodes.getLength(); i++) {

			String name = "";
			String localeDispaly = "";

			pathExpression = xpath
					.compile("//DepartmentConfig/Department/name/text()");
			NodeList nameNodes = (NodeList) pathExpression.evaluate(doc,
					XPathConstants.NODESET);
			if (nameNodes.item(i) != null) {
				name = nameNodes.item(i).getNodeValue();
			}

			pathExpression = xpath
					.compile("//DepartmentConfig/Department/localeDispaly/text()");
			NodeList localeDispalyNodes = (NodeList) pathExpression.evaluate(
					doc, XPathConstants.NODESET);
			if (localeDispalyNodes.item(i) != null) {
				localeDispaly = localeDispalyNodes.item(i).getNodeValue();
			}

			map.put(name, localeDispaly);
			LOGGER.debug(StringUtil.MapToString(map));
		}
		return map;
	}
	
	public static Map<String, String> execute(String path, String rootNodeName,
			String child1NodeName, String child2NodeName) throws Exception {

		Map<String, String> map = new HashMap<String, String>();

		XPathFactory pathFactory = XPathFactory.newInstance();
		XPath xpath = pathFactory.newXPath();
		XPathExpression pathExpression = xpath
				.compile(rootNodeName);

		Document doc = LoadingXmlUtil.execute(path);
		Object result = pathExpression.evaluate(doc, XPathConstants.NODESET);

		NodeList nodes = (NodeList) result;
		for (int i = 0; i < nodes.getLength(); i++) {

			String name = "";
			String localeDispaly = "";

			pathExpression = xpath
					.compile(rootNodeName+"/"+child1NodeName+"/text()");
			NodeList nameNodes = (NodeList) pathExpression.evaluate(doc,
					XPathConstants.NODESET);
			if (nameNodes.item(i) != null) {
				name = nameNodes.item(i).getNodeValue();
			}

			pathExpression = xpath
					.compile(rootNodeName+"/"+child2NodeName+"/text()");
			NodeList localeDispalyNodes = (NodeList) pathExpression.evaluate(
					doc, XPathConstants.NODESET);
			if (localeDispalyNodes.item(i) != null) {
				localeDispaly = localeDispalyNodes.item(i).getNodeValue();
			}

			map.put(name, localeDispaly);
			LOGGER.debug(StringUtil.MapToString(map));
		}
		return map;
	}
}
